/**
 * Created by lixiaodi on 2018/10/10.
 */
mini.parse();
var form = new mini.Form("form1");
//得到集团/公司/部门
var nowGoups = mini.getByName("groups.groupId");
var nowCompany = mini.getByName("company.companyId");
var nowDep = mini.getByName("prentName");
var prentId = mini.getByName("prentId");
//加载下拉框内容
$(function(){
    nowGoups.setUrl(ajaxUrl + "/GroupsController/getList.do");  //模块Url
    nowCompany.setUrl(ajaxUrl + "/CompanyController/getList.do");  //模块Url
    nowDep.setUrl(ajaxUrl + "/DepartmentController/getList.do");  //模块Url
});
function SaveData() {
    var ids = form.getData();
    var department = {
        departmentId : ids.departmentId,
        departmentCode : ids.departmentCode,
        departmentName: ids.departmentName,
        departmentDesc : ids.departmentDesc,
        groupId : ids.groups.groupId,
        companyId : ids.company.companyId,
        prentId : ids.prentId,
        prentName : ids.prentName,
        createDate : ids.createDate
    };
    var jsonString = JSON.stringify(department);
    $.ajax({
        url: ajaxUrl + "/DepartmentController/save.do",
        type: 'POST',
        data: jsonString,
        contentType : "application/json",
        success: function (data) {
            console.log(data)
            CloseWindow("save");
        },
        error: function (jqXHR, textStatus, errorThrown) {
            //alert(jqXHR.responseText);
            CloseWindow();
        }
    });
}
//编辑
function SetData(data) {
    if (data.action == "edit") {
        //跨页面传递的数据对象，克隆后才可以安全使用
        data = mini.clone(data);
        console.log(data)
        $.ajax({
            url: ajaxUrl + "/DepartmentController/findById.do?id=" + data.id,
            cache: false,
            success: function (data) {
                var o = mini.decode(data);
                form.setData(o.data);
                form.setChanged(false);
            }
        });
    }else if(data.action == "new"){
        var parsIds = data.parsId;
        //赋值
        nowGoups.setValue(parsIds.groupId);
        nowCompany.setValue(parsIds.companyId);
        nowDep.setValue(parsIds.depId);
        prentId.setValue(parsIds.depId);
    }
}

function GetData() {
    var o = form.getData();
    return o;
}
function CloseWindow(action) {
    if (action == "close" && form.isChanged()) {
        if (confirm("数据被修改了，是否先保存？")) {
            return false;
        }
    }
    if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
    else window.close();
}
function onOk(e) {
    SaveData();
}
function onCancel(e) {
    CloseWindow("cancel");
}
var depValue
//当父级部门修改的时候
function onDeptChanged(e) {
    depValue = nowDep.getValue();
    return depValue;
}

//重置
function onReset(){
    var ids = form.getData();
    var userId = ids.userId
    $.ajax({
        url: ajaxUrl + "/user/replacePassword.do?userId=" + userId,
        type:"post",
        success: function (data) {
            console.log(data)
        }
    });

}
